package com.treasure.mall.zbqmallapi.controller;

import com.treasure.mall.biz.dto.condition.SpecialAreaCondition;
import com.treasure.mall.biz.entity.product.SpecialAreaProductPO;
import com.treasure.mall.biz.service.core.product.SpecialAreaService;
import com.treasure.mall.biz.vo.PageDataVO;
import com.treasure.mall.biz.vo.product.ProductApiVo;
import com.treasure.mall.zbq.common.base.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/6/22
 */
@RestController
@RequestMapping("/special/area")
@Api(tags = "活动专区模块")
@CrossOrigin
public class SpecialAreaController {
    @Autowired
    private SpecialAreaService specialAreaService;

    @ApiOperation("活动专区商品列表")
    @GetMapping("/products")
    public ApiResult products(
            SpecialAreaCondition condition,
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "20") int size
    ) {
        condition.setStatusCode(1);
        Page<SpecialAreaProductPO> productPOS = specialAreaService.findAll(page, size, condition);

        PageDataVO<ProductApiVo> pageDataVO = new PageDataVO<>();
        pageDataVO.setPageSize(size);
        pageDataVO.setPageIndex(page);
        pageDataVO.setPageCount(productPOS.getTotalPages());
        pageDataVO.setTotal(productPOS.getTotalElements());
        List<ProductApiVo> productVOS = new ArrayList<>();
        productPOS.getContent().forEach(p -> productVOS.add(new ProductApiVo(p.getProductPO())));
        pageDataVO.setRows(productVOS);

        return ApiResult.ok(pageDataVO);
    }
}
